package com.example.moneygrosService;

import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.EditText;

public class CommunicationManagerService extends Service {
	private static final String TAG = CommunicationManagerService.class.getSimpleName();
	private final IBinder mBinder = new LocalBinder();

	// C'est pour du test
	public static final String MONEYGROS_BT_SERVICE_NAME = "MoneyGros-BT-Service";
	public static final UUID MONEYGROS_BT_SERVICE_UUID = UUID.fromString("3817600e-5ff4-45dc-924f-c8cb9f650320");

	private BluetoothHandler mBluetoothHandler;

	private NotificationManager nm;    
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return mBinder;
	}

	/* (non-Javadoc)
	 * @see android.app.Service#onCreate()
	 */
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		Log.d(TAG, "onCreate");
		
		super.onCreate();
		mBluetoothHandler = new BluetoothHandler(this.getApplicationContext());
	}

	/* (non-Javadoc)
	 * @see android.app.Service#onStartCommand(android.content.Intent, int, int)
	 */
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.d(TAG, "onStartCommand");
		// TODO Auto-generated method stub
		if(mBluetoothHandler != null && mBluetoothHandler.enabled()){
				
		}
		if (mBluetoothHandler== null){
			mBluetoothHandler = new BluetoothHandler(this.getApplicationContext());
		}
        showNotification();

		return super.onStartCommand(intent, flags, startId);
	}
 
	/* (non-Javadoc)
	 * @see android.app.Service#onDestroy()
	 */
	@Override
	public void onDestroy() {
		Log.d(TAG, "onDestroy");
		// TODO Auto-generated method stub
		if(mBluetoothHandler != null && mBluetoothHandler.enabled()){
			mBluetoothHandler = null;		
		}
        nm.cancel(R.string.notif_commService_started); // Cancel the persistent notification.

		super.onDestroy();
	}

	private void showNotification() {
		nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		// In this sample, we'll use the same text for the ticker and the expanded notification
		CharSequence text = getText(R.string.notif_commService_started); 
		// Set the icon, scrolling text and timestamp
		Notification notification = new Notification(R.drawable.ic_launcher, text, System.currentTimeMillis());
		// The PendingIntent to launch our activity if the user selects this notification
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivityService.class), 0);
		// Set the info for the views that show in the notification panel.
		notification.setLatestEventInfo(this, getText(R.string.notif_commService_label), text, contentIntent);
		// Send the notification.
		// We use a layout id because it is a unique number.  We use it later to cancel.
		nm.notify(R.string.notif_commService_started, notification);
	}


	public void sendMessage(String BTAddr){		
		BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(BTAddr);

		try {
			BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(MONEYGROS_BT_SERVICE_UUID);
			Log.d(TAG, "socket créée "+ socket.toString());
			socket.connect();
			OutputStream os = socket.getOutputStream();
			os.write(12);

			Log.d(TAG, "socket connectée ");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void sendChangeBalance(User user, int offSet){		
		BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(user.getBluetooth_MAC());

		// TODO 
	}
	
	public class LocalBinder extends Binder {
		CommunicationManagerService getService() {
            // Return this instance of LocalService so clients can call public methods
            return CommunicationManagerService.this;
        }
    }

}
